package com.socket.nio.client;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class MySocketClient {

    private String ip;
    private int port;

    public MySocketClient(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    public void clientSocket(byte[] b) throws IOException {
        Socket socket = new Socket(this.ip, this.port);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(b);
        outputStream.close();
        socket.close();
    }

    public static void main(String[] args) {
        MySocketClient mySocketClient = new MySocketClient("127.0.0.1", 8080);
        try {
            int i = 0;
            while (true) {
                i++;
                String str = String.valueOf(i);
                mySocketClient.clientSocket(str.getBytes());
                Thread.sleep(5000);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Demo {
    private String name;

    Demo(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}